<template>
  <div class="home-page">
    <van-tabs animated v-model="activeIndex">
  <van-tab v-for="item in myChannels" :title='item.name ' :key="item.id">
    <ArticleList  :channelId="item.id"></ArticleList>
  </van-tab>
</van-tabs>

   <!-- 按钮 -->
   <div class="btn-wrapper">
      <geek-icon name="search"></geek-icon>
      <geek-icon name="channel" @click.native="showChannel=true"></geek-icon>
    </div>
    <article-channel
    v-model="showChannel"
    :myChannels="myChannels"
    :activeIndex.sync="activeIndex"
    @addMyChannels="addMyChannels"
    @spliceMyChannels="spliceMyChannels"
    >
  </article-channel>
  </div>
</template>
<script>
import { getAllChannels, getMyChannels } from '@/api/channel'
import ArticleList from './components/article-list.vue'
import ArticleChannel from './components/article-channel.vue'
export default {
  components: {
    ArticleList,
    ArticleChannel
  },
  name: 'HomePage',
  created () {
    this.getAllChannels()
    this.getMyChannels()
  },
  data () {
    return {
      myChannels: [],
      // 控制频道组件显示隐藏
      showChannel: false,
      activeIndex: 0
    }
  },
  methods: {
    async getAllChannels () {
      const [, res] = await getAllChannels()
      console.log(res)
    },
    addMyChannels (value) {
      this.myChannels.push(value)
    },

    async getMyChannels () {
      const channels = await getMyChannels()
      this.myChannels = channels
    },
    spliceMyChannels (index) {
      this.myChannels.splice(index, 1)
    }
  },
  watch: {
    '$store.state.user.token': async function () {
      const channels = await getMyChannels()
      this.myChannels = channels
      this.activeIndex = 0
    }
  }
}
</script>
<style  scoped lang="less">
::v-deep .van-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
   height: 100%;
  display: flex;
 flex-direction: column;
  .van-tabs__line {
    background: @geek-color;
    height: 2px;
    width: 32px;
  }
  .van-tab {
    color: #9EA1AE;
  }
  .van-tab--active {
    font-size: 18px;
    color: #333;
  }
  .van-tabs__wrap {
    padding-right: 86px;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
  }
  .van-tabs__content {
    flex: 1;
    overflow: hidden;
  }
  .van-tab__pane {
    height: 100%;
  }

  .van-tabs__content {
    flex: 1;
    overflow: hidden;
  }
  .van-tab__pane {
    height: 100%;
  }

}

.home-page {
  .btn-wrapper {
    position: absolute;
    right: 0;
    top: 0;
    width: 86px;
    height: 44px;
    background: #fff;
    display: flex;
    align-items: center;
    .geek-icon {
      flex: 1;
      text-align: center;
      font-size: 18px;
    }
    &::before {
      content: "";
      width: 20px;
      height: 44px;
      position: absolute;
      left: -20px;
      top: 0;
      background: linear-gradient(to right, rgba(255,255,255,0), #fff);
    }
  }
}
</style>
